
package org.socratons.service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.japril.servlet.WebAppUser;
import org.japril.servlet.xmlservlet.EngineHandler;
import org.japril.servlet.xmlservlet.EnvironmentInitializer;
import org.japril.servlet.xmlservlet.ResponseHandler;
import org.xml.sax.SAXException;

import com.google.appengine.api.rdbms.AppEngineDriver;

public final class SocratonsHandler implements EnvironmentInitializer,
  EngineHandler, ResponseHandler
{
  private String[] m_res=new String[]{ "text/plain", "ok" };
  
  // Specified in ResponseHandler.
  public void perform(WebAppUser wau, String xml, Object o, 
    HttpServletResponse resp) throws IOException 
  {
    resp.setContentType(m_res[0]);  
    PrintWriter pw=resp.getWriter();
    pw.println(m_res[1]);
    pw.flush();
  }
  
  // Specified in EngineHandler.
  public void perform(String xml, Object o) throws IOException
  {
    ByteArrayInputStream bais=new ByteArrayInputStream(xml.getBytes());
    
    SAXParserFactory f=SAXParserFactory.newInstance();
    f.setValidating(false);
    f.setNamespaceAware(false);
    
    try
    {
      SAXParser parser=f.newSAXParser();
      
      SocratonsXmlProtocolSaxHandler h=new SocratonsXmlProtocolSaxHandler();
      parser.parse(bais, h);
      
      m_res=performMethod(h.getMethod(), h.getParsed());
    }
    catch (ParserConfigurationException pcx)
    {
      m_res[0]="text/plain";
      m_res[1]=pcx.getLocalizedMessage();
    }
    catch (SAXException saxx)
    {
      m_res[0]="text/plain";
      m_res[1]=saxx.getLocalizedMessage();
    }
    
  }
  
  /*
  @Override
  public void perform(String xml, Object parsed, Object o) throws IOException {
  	// TODO Auto-generated method stub
  	
  }
  */
  
  private String[] performMethod(String method, List<SocData> sd)
  {
    String res="ok";
    
    try
    {
      DriverManager.registerDriver(new AppEngineDriver());
      Connection c=DriverManager.getConnection(
        "jdbc:google:rdbms://ahaspaces:ahaxdb/gim");
      
      SocratonsAccess sa=new SocratonsAccess(c);
    
      if (method.equals("update"))
      {
        for (SocData s : sd) res=sa.update(s.id, s.bisons);
        
        return new String[]{ "text/plain", res };
      }
      else if (method.equals("copy"))
      {
        res=sa.copy();
        
        return new String[]{ "text/xml", res };
      }
      else
      {
        res="uknown method: "+method;
      }
    }
    catch (SQLException iox)
    {
      res=iox.getLocalizedMessage();
    }
    catch (IOException iox)
    {
      res=iox.getLocalizedMessage();
    }
    
    return new String[]{ "text/plain", res };
  }

  // Specified in EnvironmentInitializer.
  public EngineHandler getEngineHandler(){ return this; }
  
  // Specified in EnvironmentInitializer.
  public ResponseHandler getResponseHandler(){ return this; }

  // Specified in EnvironmentInitializer.
  public Object initialize(String xml) throws IOException { return xml; }
  
}
